//
//  WCDatePickerView.m
//  WC
//
//  Created by 杨鑫 on 15/5/4.
//  Copyright (c) 2015年 智康. All rights reserved.
//

#import "WCDatePickerView.h"
#import "WCMaskView.h"
#import "Macros.h"

@interface WCDatePickerView ()

@property (weak, nonatomic) WCMaskView *maskView;

@end

@implementation WCDatePickerView

- (instancetype)init
{
    return [self initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260)];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260)];
    if (self) {
        [self loadSubView];
    }
    return self;
}

- (void)loadSubView
{
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    toolbar.barTintColor = [UIColor whiteColor];
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnTap)];
    leftBtn.tintColor = kTxtColor;
    
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnTap)];
    rigthBtn.tintColor = kTxtColor;
    
    [toolbar setItems:@[leftBtn, flex, rigthBtn]];
    
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(66, 0, kScreenWidth-132, 44)];
    self.dateLabel = dateLabel;
    dateLabel.textAlignment = NSTextAlignmentCenter;
    dateLabel.font = [UIFont boldSystemFontOfSize:20.0];
    
    [toolbar addSubview:dateLabel];
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, 216)];
    self.datePicker = datePicker;
    datePicker.backgroundColor = kAppBg;
    datePicker.datePickerMode = UIDatePickerModeDate;
    
    [self addSubview:toolbar];
    [self addSubview:datePicker];
}

- (void)leftBtnTap
{
    [self hide:nil];
}

- (void)rightBtnTap
{
    if (self.close) {
        self.close(self.datePicker.date);
    }
    [self hide:self.save];
}

- (void)show:(UIViewController *)vc completion:(void (^)(BOOL finished))completion
{
    WCMaskView *maskView = [[WCMaskView alloc] init];
    self.maskView = maskView;
    
    [maskView addSubview:self];
    [vc.view addSubview:self.maskView];
    
    __weak UIView *weakSelf = self;
    [UIView animateWithDuration:.25 delay:.0 options:7<<16 animations:^{
        weakSelf.frame = CGRectMake(0, kScreenHeight-260, kScreenWidth, 260);
    } completion:completion];
}

- (void)hide:(void (^)(BOOL finished))completion
{
    __weak UIView *weakSelf = self;
    [UIView animateWithDuration:.25 delay:.0 options:7<<16 animations:^{
        weakSelf.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 260);
    } completion:^(BOOL finished){
        if (completion) {
            completion(finished);
        }
        [weakSelf.maskView removeFromSuperview];
    }];
}

@end
